<template>
  <div class="station-top">
    <div v-if="siteGroup?.length > 0" class="site-groups" @click="showDialog = true">
      <!-- <i class="iconfont">&#xeb49;</i> -->
      <i class="iconfont sync-icon" title="手动同步站点内容到es搜索引擎" @click.stop="handleSync">&#xe675;</i>
      <p :title="currentSite.name">
        {{ currentSite.name }}
      </p>
      <i class="iconfont arrow-down-icon" @click="showDialog = true">&#xe601;</i>
    </div>

    <el-dialog v-model="showDialog" title="站点目录" width="30%" append-to-body align-center>
      <div class="site-list">
        <div v-for="item in siteGroup" :key="item.id" class="site-item">
          <div class="site-info">
            <div class="site-name">
              {{ item.name }}
              <el-tag :type="item.state === 0 ? 'success' : 'danger'" size="small" effect="light" class="state-tag">
                {{ item.state === 0 ? '运行中' : '已关停' }}
              </el-tag>
            </div>
            <a class="site-url" :href="item.domain + item.uri" target="_blank">{{ item.domain + item.uri }}</a>
          </div>
          <div class="site-action">
            <el-button v-if="currentSite.id === item.id" size="small" type="info" disabled>当前站点</el-button>
            <el-button v-else size="small" type="primary" @click="handleChangeSite(item)">切换站点</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { syncSiteContentToEs } from '@/api/content'

const router = useRouter()
const store = useStore()
const showDialog = ref(false)
const currentSite = computed(() => store.state.site.currentSite || {})
const siteGroup = computed(() => store.state.site.siteGroup)

const handleChangeSite = site => {
  if (String(currentSite.value.id) !== String(site.id)) {
    store.commit('site/SET_CURRENT_SITE', site)
    store.commit('category/SET_SITE_CATEGORY_TREE', {siteId: site.id})
    showDialog.value = false
    if (router.currentRoute.value.path.includes('/stationContent')) {
      router.push({ path: '/content/contentManage/stationContent' })
      setTimeout(() => {
        router.go(0)
      }, 0)
    } else {
      router.go(0)
    }
  }
}

const handleSync = async () => {
  const res = await syncSiteContentToEs({siteId: currentSite.value.id})
  if (res.code === 20000) {
    ElMessage.success('同步成功')
  } else {
    ElMessage.error('同步失败')
  }
}
</script>

<style scoped lang="scss">
.station-top {
  display: flex;
  align-items: center;
  margin-left: 30px;
  height: 100%;

  .site-groups {
    height: 30px;
    background-color: $focusContent;
    border-radius: 5px;
    color: $mainColor;
    cursor: pointer;
    padding: 0 15px;
    display: flex;
    align-items: center;
    p {
      @include textEllipsis(30px, 1);
      font-size: 14px;
    }
    i {
      font-size: 18px;
      color: $mainColor;
      margin-right: 5px;
    }
    .arrow-down-icon {
      margin-left: 10px;
    }
    .sync-icon {
      cursor: pointer;
      font-size: 14px;
    }
  }
}

.site-list {
  max-height: 60vh;
  overflow-y: auto;

  .site-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 0;
    border-bottom: 1px solid #eee;

    &:last-child {
      border-bottom: none;
    }

    .site-info {
      flex: 1;
      margin-right: 16px;

      &:hover {
        .site-url {
          color: var(--el-color-primary);
        }
      }

      .site-name {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 16px;
        font-weight: 500;
        color: #303133;
        margin-bottom: 8px;
        transition: color 0.3s;

        .state-tag {
          font-weight: normal;
        }
      }

      .site-url {
        font-size: 13px;
        color: #909399;
        cursor: pointer;
      }
    }

    .site-action {
      flex-shrink: 0;
    }
  }
}

:deep(.el-dialog__header) {
  margin-right: 0;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

:deep(.el-dialog__body) {
  padding: 0;
}

:deep(.el-dialog__headerbtn) {
  top: 20px;
}
</style>
